package com.hotel.controller;

import com.alibaba.fastjson.JSONObject;
import com.hotel.component.BaseController;
import com.hotel.component.IocContainer;
import com.hotel.entity.Option;
import com.hotel.entity.PageBean;
import com.hotel.entity.Room;
import com.hotel.exception.EntityException;
import com.hotel.service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

/**
 * @Author Str
 * @Date 2021/10/28 21:22
 * @Description :房间管理
 */
@WebServlet("/room/*")
public class RoomController extends BaseController {
    private RoomService roomService = (RoomService) IocContainer.getBean("RoomService");

    // 跳转到列表
    public String viewList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/room/list.jsp";
    }

    // 跳转到新增页面
    public String viewAddRoom(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/room/addRoom.jsp";
    }

    // 获取房间状态
    public String getStatusData(HttpServletRequest req, HttpServletResponse resp) {
        List<Option> options = new ArrayList<>();
        List<Room> status = null;
        JSONObject jsObj = null;
        try {
            jsObj = new JSONObject();
            status = roomService.getRooms();
            Set<String> set=new HashSet<>();
//            System.out.println(status);
            // 将楼层和id填充进Option列表
            for (Room s : status) {
                set.add(s.getStatus());
            }
            for (String s : set) {
                Option op = new Option(s,s);
                options.add(op);
            }
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            jsObj.put("result", "failed");
            e.printStackTrace();
        }
        return "data:" + jsObj;
    }

    //任务(4)-新增 or 更新一个用户。
    //映射地址: /room/saveRoom
    public String saveRoom(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        Room room = makeRoom(req);
        System.out.println("room============"+room);
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            roomService.save(room);
            jsObj.put("result", "success");
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }


    // 将返回的数据封装成对象
    public Room makeRoom(HttpServletRequest req) {
        System.out.println("+----------------------------------------------------+");
        System.out.println( req.getParameter("typeId") );
        System.out.println("+----------------------------------------------------+");

        Room room = new Room();
        //{1}修改时使用。
        room.setId(req.getParameter("id"));
        //{2}新增时使用。
        room.setRoomNo(req.getParameter("roomNo"));
        room.setTypeId(req.getParameter("typeId"));
        room.setStoreyId(req.getParameter("storeyId"));
        room.setStatus(req.getParameter("status"));
        room.setTypeName(req.getParameter("typeName"));
        room.setFloor(req.getParameter("floor"));
        room.setDayPrice( parseFloat(req.getParameter("dayPrice")) );
        return room;
    }

    public String delRoom(HttpServletRequest req, HttpServletResponse resp){
        JSONObject jo = new JSONObject();
        String id = req.getParameter("id");
        String respTxt = "";
        try {
            roomService.delete(id);
            jo.put("result", "success");
            respTxt = jo.toString();
        } catch (SQLException | EntityException e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    /**
     * 映射地址: /Notify/jsonList
     * 提交参数: page, limit, ...
     *
     * @return java.lang.String
     */
    public String jsonList(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        //{1}获取前端提交的关键字 [调BaseController的方法]
        Map<String, Object> keyWords = getKeyWords(req);
        for (String s : keyWords.keySet()) {
            System.out.println("key::" + s + ",value::" + keyWords.get(s));
        }
        String respTxt = "";
        PageBean<Room> page = null;
        try {
            //{2}调用 service 方法查询数据(传入关键字)
            page = roomService.getList(keyWords);
            //{3}转成 json 格式数据.
            respTxt = page.toJSON();
        } catch (Exception e) {
            e.printStackTrace();  //包装错误信息..
            respTxt = packErrMsg(e);
        }
        //{4}返回数据到 LayUI 表格组件。
        return "data:" + respTxt;
    }

    //任务(3)-获取根据 id 获取一个用户
    //映射地址: /User/getUser?id=xxx
    public String getRoom(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        //{1}获取 id 的值
        String id = req.getParameter("id");
        Room room = null;
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            //{2}根据 id 获取用户
            room = roomService.getById(id);
            //{3}存入相关信息
            jsObj.put("result", "success");
            jsObj.put("room", room);
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    private String packErrMsg(Exception e) {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code", -1);
        jsObj.put("cause", e.getMessage());
        jsObj.put("result", "failed");
        //{ps}如果还有其它后面再加...
        return jsObj.toString();
    }
}
